$(function () {
    var ljkmdmys = {
        grid:$("#ljkmdmys_grid"),
        gridOptions:{
            url:$.webPath() + "/cwgl/kmys/ljkmdmys/ljkmdmysList",
//            updateUrl:$.webPath() +'/cwgl/kmys/ljkmdmys/save',
            toolbar:'#toolbar_ljkmdmys',
            fitColumns:true,
            fit:true,
            rownumbers:true,
            pageNumber:1,
            pagination:true,
            singleSelect:true,
            columns:[
                [
                    {field:'ljkmdmys.qyjgId', title:'企业机构Id', hidden:true},
                    {field:'LJKMDMYS_QYJGQC', title:'企业机构', width:100},
                    {field:'ljkmdmys.type', title:'类型', width:100,formatter:function (value, row, index){
                        if(value == 'wls') {
                            return '物流商';
                        } else {
                            return '企业';
                        }
                    }},
                    {field:'ljkmdmys.gysfzhsdm', title:"供应商辅助核算代码", editor:{type:'text'}, width:100},
                    {field:'ljkmdmys.khfzhsdm', title:"客户辅助核算代码", editor:{type:'text'}, width:100},
                    {field:'cz', title:'操作', formatter:function (value, row, index) {
                        if (value && value == 'save')
                            return '<a index="' + index + '" name="save" href="javascript:void(0)" onclick="save_ljkmdmys(' + index + ')"></a>'
                        return '<a index="' + index + '" name="edit" href="javascript:void(0)" onclick="edit_ljkmdmys(' + index + ')"></a>'
                    }, width:30, align:'center'}
                ]
            ],
            onLoadSuccess:function () {
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function (rowIndex, rowData) {
                ljkmdmys.grid.edatagrid("updateRow", {
                    index:rowIndex,
                    row:$.extend({}, rowData, {cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit:function (index, row) {
                var url = $.webPath() + '/cwgl/kmys/ljkmdmys/save';
                $.post(url, row, function (data) {
                    if (data.statusCode == 200) {
                        ljkmdmys.grid.datagrid('updateRow', {
                            index:index,
                            row:$.extend({}, row, {cz:'edit','ljkmdmys.id':data.id})
                        });
                        $("a[name='edit']").linkbutton({
                            plain:true,
                            iconCls:'icon-edit'
                        });
                    } else {
                        $.messager.alert('info', data.message);
                        ljkmdmys.grid.edatagrid("beginEdit", index);
                    }
                }, 'json');
            }
        },
//        save:function(){},
        search:function () {
//            var qc = $('#ljkmdmys_qyjg').attr("_text") == null ? null : $('#ljkmdmys_qyjg').attr("_text");
            ljkmdmys.grid.datagrid("load", {
                qyjgQc:$("#ljkmdmys_qyjg").val(),
                gysfzhsdm:$("#ljkmdmys_gysfzhsdm").val() || '',
                khfzhsdm:$("#ljkmdmys_khfzhsdm").val() || ''
            });
        }
    }
    ljkmdmys.grid.edatagrid(ljkmdmys.gridOptions);
//    $("#ljkmdmys_qyjg").gsjgqczcombobox();
    $("#btn_ljkmdmys_search").click(function () {
        ljkmdmys.search();
    });
});
function save_ljkmdmys(index) {
    $("#ljkmdmys_grid").edatagrid("endEdit", index);
}
function edit_ljkmdmys(index) {
    $("#ljkmdmys_grid").edatagrid("beginEdit", index);
}